using System;
using System.Windows;
using System.Windows.Navigation;
using WcfService1.DTO;

namespace Lib.Helper
{
    public static class Navigation
    {
        
        private static NavigationService _MyNavigation;
        private static string _PrevUri; 
        
        public static NavigationService MyNavigation
        {
            get
            {
                _MyNavigation.Navigating += new NavigatingCancelEventHandler(_MyNavigation_Navigating);
                 _MyNavigation.Navigated += new NavigatedEventHandler(_MyNavigation_Navigated);
                return _MyNavigation;
            }
            set
            {
                if (value == null)
                {
                    return;
                }
                   
                _MyNavigation = value;
                _MyNavigation.Navigating += new NavigatingCancelEventHandler(_MyNavigation_Navigating);
                _MyNavigation.Navigated += new NavigatedEventHandler(_MyNavigation_Navigated);
            }
        }

        static void _MyNavigation_Navigated(object sender, NavigationEventArgs e)
        {
            _MyNavigation.Navigated -= new NavigatedEventHandler(_MyNavigation_Navigated);
            _PrevUri = e.Uri.ToString().ToLower();
        }

        static void _MyNavigation_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            _MyNavigation.Navigating -= new NavigatingCancelEventHandler(_MyNavigation_Navigating);
            if (_PrevUri !=null )
            {
                var urlDestination = e.Uri.ToString().Split(new string[] {"/"}, StringSplitOptions.RemoveEmptyEntries);
                var urlSource = _PrevUri.ToString().Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);

                if (_PrevUri.Contains("add"))
                    if (urlDestination[0].ToLower() != urlSource[0].ToLower())
                    {
                        MessageBox.Show("You have to Cancel or Save this transaction!");
                        e.Cancel = true;
                    }
            }
        }

        public static Object Args { get; set; }
        public static UserDTO User { get; set; }
        //Untuk Pemisahan KodeTransaksi berdasarkan User Departemen
        public static string Dep { get; set; }
        public static string ReportUrl { get; set; }
        public static Socket OkaSocket { get; set; }
        public static string CurrentTransaction { get; set; }
    }
}
